package com.dd.cloud.user.service.drugs.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.PharConstants;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.BigDecimalUtils;
import com.dd.cloud.common.utils.ExtUtils;
import com.dd.cloud.user.entity.drugs.DrugsOutbound;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.drugs.DrugsOutboundMapper;
import com.dd.cloud.user.req.drugs.DrugsOutboundGroupPageReq;
import com.dd.cloud.user.req.drugs.DrugsOutboundOrderPageReq;
import com.dd.cloud.user.res.drugs.DrugsOutboundGroupPageRes;
import com.dd.cloud.user.res.drugs.DrugsOutboundOrderPageRes;
import com.dd.cloud.user.res.drugs.DrugsOutboundOrderSummary;
import com.dd.cloud.user.res.drugs.SummaryPage;
import com.dd.cloud.user.res.order.OrderPresDetailExcelRes;
import com.dd.cloud.user.service.drugs.IDrugsOutboundService;
import com.dd.cloud.user.service.phar.IPharmacyService;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.dd.cloud.common.vo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 药品出库记录表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-04-18
 */
@Service
public class DrugsOutboundServiceImpl extends ServiceImpl<DrugsOutboundMapper, DrugsOutbound> implements IDrugsOutboundService {

    @Autowired
    private IPharmacyService pharmacyService;

    /**
     * @param req:
     * @return Page<DrugsOutboundPageRes>
     * @description 分页查询药品出库记录 按药品分组记录
     * @author czg
     * @date 2023/4/19 19:10
     */
    @Override
    public Page<DrugsOutboundGroupPageRes> getDrugsOutboundPage(DrugsOutboundGroupPageReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();

        if (loginInfo.getType() == LoginInfo.UserType.PHAR) {
            req.setPharId(new Integer[]{loginInfo.getPharId()});
        }
        Page<DrugsOutboundGroupPageRes> page = req.pagePojo();
        page = getBaseMapper().getDrugsOutboundPage(page, req);
        return page;
    }

    @Autowired
    private IPresPrescriptionService presPrescriptionService;

    /**
     * @param req:
     * @return Page<DrugsOutboundOrderPageRes>
     * @description 分页查询药品出库记录, 按订单查询
     * @author czg
     * @date 2023/4/19 19:18
     */
    @Override
    public SummaryPage<DrugsOutboundOrderPageRes> getDrugsOutboundOrderPage(DrugsOutboundOrderPageReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        if (loginInfo.getType() == LoginInfo.UserType.PHAR) {
            req.setPharId(loginInfo.getPharId());
        }
        SummaryPage<DrugsOutboundOrderPageRes> page = new SummaryPage<>(req.getPage(), req.getPageSize());
        page = getBaseMapper().getDrugsOutboundOrderPage(page, req);
        if (req.getStartTime() == null) {
            req.setStartTime(LocalDate.of(2023, 1, 1));
        }
        if (req.getEndTime() == null) {
            req.setEndTime(LocalDate.now());
        }
        List<DrugsOutboundOrderSummary> list = getBaseMapper().getDrugsOutboundSummary(req);
        page.setSummary(list);
        return page;
    }

    /**
     * @description excel导出--分页查询出库记录 按药品分组查询
     * @param req: 查询的参数
     * @return void
     * @author czg
     * @date 2023/6/16 10:51
    */
    @Override
    public void exportDrugsOutboundExcel(DrugsOutboundGroupPageReq req, HttpServletResponse response) {
        Page<DrugsOutboundGroupPageRes> drugsOutboundPage = getDrugsOutboundPage(req);
        List<DrugsOutboundGroupPageRes> list = drugsOutboundPage.getRecords();
        list = list.stream().map(item ->{
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            item.setOutboundPrice(BigDecimalUtils.divide(item.getOutboundPrice(), new BigDecimal(100),6));
            if (req.getStartTime() == null || req.getEndTime() == null){
                item.setTimePeriod(item.getCreateDate().format(df) + "--" + LocalDate.now().format(df));
            } else {
                item.setTimePeriod(req.getStartTime().format(df) + "--" + req.getEndTime().format(df));
            }
            return item;
        }).collect(Collectors.toList());
        String title = "药品出库数量统计报表";
        try {
            ExtUtils.exportExcel(list, title, title, DrugsOutboundGroupPageRes.class, title, true, response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException("导出失败");
        }
    }

    /**
     * @description excel导出--分页查询出库记录 按订单分组查询
     * @param req:
    	 * @param response:
     * @return void
     * @author czg
     * @date 2023/6/16 11:31
    */
    @Override
    public void exportOrderOutboundExcel(DrugsOutboundOrderPageReq req, HttpServletResponse response) {
        Page<DrugsOutboundOrderPageRes> drugsOutboundPage = getDrugsOutboundOrderPage(req);
        List<DrugsOutboundOrderPageRes> list = drugsOutboundPage.getRecords();
        list = list.stream().map(item ->{
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            item.setOutboundPrice(BigDecimalUtils.divide(item.getOutboundPrice(), new BigDecimal(100),6));
            item.setPrice(BigDecimalUtils.divide(item.getPrice(), new BigDecimal(100),6));
            return item;
        }).collect(Collectors.toList());
        String title = "药房名称-药品名称出库记录报表";
        try {
            ExtUtils.exportExcel(list, title, title, DrugsOutboundOrderPageRes.class, title, true, response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException("导出失败");
        }
    }
}
